// pages/ucenter/information/information.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headerImg: [],
    picUrls: [],
    hidden: true,
    // name:'banana',
    // sex:'男',
    flag: '',
    titles: '修改个人信息',
    userInfo: {
      avatarUrl: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'
    },
    name: null,
    sex: null,
  },
  //跳转到修改密码
  toUpdatePassword: function() {
    wx.navigateTo({
      url: '/pages/auth/reset/reset',
    })
  },
   //获取个人资料
  getInformation: function(e) {
    let userInfo = wx.getStorageSync('userInfo');
    this.setData({
      userInfo: userInfo,
    });
    console.log(userInfo)
  },
  //修改个人资料
  updateInformation: function() {
    let that = this
    util.request(api.updateInformation, {
      username: that.data.name,
      gender: that.data.sex,
      avatar: that.data.picUrls[0]
    }, 'POST').then(res => {
      if (res.errno == 0) {
        let userInfo = that.data.userInfo
        if (that.data.name != null) {
          userInfo.nickName = that.data.name
          console.log('name')
        }

        if (that.data.sex != null) {
          userInfo.gender = that.data.sex
          console.log('sex')
        }

        if (that.data.picUrls.length > 0) {
          userInfo.avatarUrl = that.data.picUrls[0]
          console.log('picUrls')
        }
        console.log(that.data.userInfo)
        that.setData({
          hidden: true,
          userInfo: userInfo
        })
        wx.setStorageSync('userInfo', userInfo)
        wx.showToast({
          title: '修改成功',
          icon: 'success'
        })
      }
    }).catch(err => {
      wx.showToast({
        title: '修改失败',
        icon: 'fail'
      })
    })
  },
  //选择图片
  selectImg: function(e) {
    // if (this.data.headerImg.length >= 1) {
    //   util.showErrorToast('只能上传一张图片')
    //   return false;
    // }
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        that.setData({
          headerImg: res.tempFilePaths
        })
        // wx.setStorageSync('headerImg', res.tempFilePaths)
        that.uploadImg(res)
      },
    })
  },
  //上传图片
  uploadImg: function(res) {
    let that = this
    wx.uploadFile({
      url: api.StorageUpload,
      filePath: res.tempFilePaths[0],
      name: 'file',
      success: function(res) {
        var _res = JSON.parse(res.data);
        if (_res.errno === 0) {
          var url = _res.data.url
          that.data.picUrls.push(url)
          that.setData({
            picUrls: that.data.picUrls
          })
          console.log(that.data.picUrls)
          that.updateInformation()
        }
      },
      fail: function(e) {
        wx.showModal({
          title: '错误',
          content: '上传失败',
          showCancel: false,
          isMale: true
        })
      },
    })
  },
  //显示弹框
  showFrame: function(e) {
    let flag = e.currentTarget.dataset.flag;
    let titles = '';
    if (flag == 'name') {
      titles = '修改昵称'
    } else {
      titles = '修改性别'
    }
    this.setData({
      hidden: false,
      flag: flag,
      titles: titles
    })
  },
  //确定修改
  confirm: function(e) {
    let that = this
    if (this.data.flag == 'name' && this.data.name == null) {
      util.showErrorToast('修改名字与之前相同！')
      return
    } else if (this.data.flag == 'sex' && this.data.sex == null) {
      util.showErrorToast('修改性别与之前相同！')
      return
    }
    that.updateInformation()


  },
  //取消修改
  cancel: function(e) {
    this.setData({
      hidden: true
    })
  },
  //设置昵称
  setName: function(e) {
    // let userInfo = this.data.userInfo
    // userInfo.nickName = e.detail.value
    this.setData({
      name: e.detail.value
    })
    console.log('name:' + this.data.name)
    // console.log(that.data.userInfo)
    // wx.setStorageSync('userInfo', userInfo)
  },
  //设置性别
  setSex: function(e) {
    // let userInfo = this.data.userInfo
    // userInfo.gender = e.detail.value
    this.setData({
      sex: e.detail.value
    })
    console.log('name:' + this.data.sex)
    // console.log(that.data.userInfo)
    // wx.setStorageSync('userInfo', userInfo)
    // wx.setStorageSync('sex', e.detail.value)
  },
  //跳转到收货地址
  toAddress: function() {
    wx.navigateTo({
      url: '/pages/ucenter/address/address',
    })
  },
  //跳转到帮助
  goHelp: function() {
    wx.navigateTo({
      url: '/pages/ucenter/help/help',
    })
  },
  //退出登录
  exitLogin: function() {
    console.log("退出")
    wx.showModal({
      title: '',
      confirmColor: '#b4282d',
      content: '退出登录？',
      success: function(res) {
        if (res.confirm) {
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
          wx.navigateTo({
            url: '/pages/auth/login/login'
          });
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.clearData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // this.saveUpdateData();
    this.getInformation();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // this.saveUpdateData()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})